package server.business.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import server.business.mapper.ReturnBloodInformationMapper;
import server.business.mapper.ReturnBloodMapper;
import server.business.mapper.ReturnBloodReviewMapper;
import server.business.pojo.BloodTransferInformation;
import server.business.pojo.ReturnBlood;
import server.business.pojo.ReturnBloodInformation;
import server.business.pojo.ReturnBloodReview;
import server.business.service.BloodInformationService;
import server.business.service.BloodTransferInformationService;
import server.business.service.BloodTransferService;
import server.business.service.ReturnBloodService;

import java.util.List;

@Service
public class ReturnBloodServiceImpl extends ServiceImpl<ReturnBloodMapper, ReturnBlood> implements ReturnBloodService {

    @Autowired
    private ReturnBloodMapper returnBloodMapper;
    @Autowired
    private ReturnBloodInformationMapper returnBloodInformationMapper;
    @Autowired
    private BloodTransferService bloodTransferService;
    @Autowired
    private BloodTransferInformationService bloodTransferInformationService;
    @Autowired
    private ReturnBloodReviewMapper reviewMapper;
    @Autowired
    private BloodInformationService bloodInformationService;



    @Override
    public IPage<ReturnBlood> getPage(int currentPage, int pageSize, ReturnBlood returnBlood) {
        LambdaQueryWrapper<ReturnBlood> queryWrapper = new LambdaQueryWrapper<ReturnBlood>();
        if(Strings.isEmpty(returnBlood.getApplyId())&&Strings.isEmpty(returnBlood.getApplyHospitalName())&&Strings.isEmpty(returnBlood.getState())){
            //默认返回属于“待审核”状态的申请信息
            returnBlood.setState("0");
            System.out.println("******************");
        }
        queryWrapper
                .eq(Strings.isNotBlank(returnBlood.getApplyId()), ReturnBlood::getApplyId,returnBlood.getApplyId())
                .eq(Strings.isNotBlank(returnBlood.getApplyHospitalName()),
                        ReturnBlood::getApplyHospitalName,returnBlood.getApplyHospitalName())
                .eq(Strings.isNotBlank(returnBlood.getState()), ReturnBlood::getState
                        ,returnBlood.getState());
//                .ge(bloodTransfer.getApplyTime() != null,BloodTransfer::getApplyTime,bloodTransfer.getApplyTime())
//                .le(bloodTransfer.getApplyTime() != null,BloodTransfer::getApplyTime,bloodTransfer.getApplyTime());
        IPage page = new Page(currentPage,pageSize);
        returnBloodMapper.selectPage(page,queryWrapper);
        return page;
    }


    @Override
    public void reject(ReturnBlood returnBlood) {
        LambdaUpdateWrapper<ReturnBlood> updateWrapper = new LambdaUpdateWrapper<ReturnBlood>();
        updateWrapper.eq(ReturnBlood::getApplyId,returnBlood.getApplyId());
        updateWrapper.set(ReturnBlood::getAuditorId,returnBlood.getAuditorId());
        updateWrapper.set(ReturnBlood::getAuditTime,returnBlood.getAuditTime());
        updateWrapper.set(ReturnBlood::getState,"2");
        updateWrapper.set(ReturnBlood::getRejectReason,returnBlood.getRejectReason());
        returnBloodMapper.update(null,updateWrapper);
        //review
        LambdaUpdateWrapper<ReturnBloodReview> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(ReturnBloodReview::getReturnBloodId,returnBlood.getApplyId());
        updateWrapper1.set(ReturnBloodReview::getReviewerId,returnBlood.getAuditorId());
        updateWrapper1.set(ReturnBloodReview::getReviewTime,returnBlood.getAuditTime());
        updateWrapper1.set(ReturnBloodReview::getState,"2");
        updateWrapper1.set(ReturnBloodReview::getRejectReason,returnBlood.getRejectReason());
        reviewMapper.update(null,updateWrapper1);
    }

    /**
     * 审核通过
     * @param
     */
    @Override
    public void approve(ReturnBlood returnBlood) {
        LambdaUpdateWrapper<ReturnBlood> updateWrapper = new LambdaUpdateWrapper<ReturnBlood>();
        updateWrapper.eq(ReturnBlood::getApplyId,returnBlood.getApplyId());
        updateWrapper.set(ReturnBlood::getAuditorId,returnBlood.getAuditorId());
        updateWrapper.set(ReturnBlood::getAuditTime,returnBlood.getAuditTime());
        updateWrapper.set(ReturnBlood::getState,"1");
        returnBloodMapper.update(null,updateWrapper);
        //更改血液状态
        LambdaQueryWrapper<ReturnBloodInformation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReturnBloodInformation::getApplyId,returnBlood.getApplyId());
        List<ReturnBloodInformation> bloodList = returnBloodInformationMapper.selectList(queryWrapper);
        for (ReturnBloodInformation blood:bloodList){
            bloodInformationService.setStateByBloodNumber(blood.getBloodNumber(),"31");//32：调血退血审批通过
        }
        //review
        LambdaUpdateWrapper<ReturnBloodReview> updateWrapper1 = new LambdaUpdateWrapper<>();
        updateWrapper1.eq(ReturnBloodReview::getReturnBloodId,returnBlood.getApplyId());
        updateWrapper1.set(ReturnBloodReview::getReviewerId,returnBlood.getAuditorId());
        updateWrapper1.set(ReturnBloodReview::getReviewTime,returnBlood.getAuditTime());
        updateWrapper1.set(ReturnBloodReview::getState,"1");
        reviewMapper.update(null,updateWrapper1);
    }

    @Override
    public void addBlood(String applyId,String bloodId) {
        BloodTransferInformation blood = bloodTransferInformationService.getBloodInformation(bloodId);
        ReturnBloodInformation bloodInformation = new ReturnBloodInformation();
        bloodInformation.setBloodNumber(blood.getBloodNumber());
        bloodInformation.setBloodComponent(blood.getBloodComponent());
        bloodInformation.setBloodBagNumber(blood.getBloodBagNumber());
        bloodInformation.setExtendCode(blood.getExtendCode());
        bloodInformation.setAboType(blood.getAboType());
        bloodInformation.setRhType(blood.getRhType());
        bloodInformation.setSpecification(blood.getSpecification());
        bloodInformation.setCollectTime(blood.getCollectTime());
        bloodInformation.setFailureTime(blood.getFailureTime());
        bloodInformation.setBloodAmount(blood.getBloodAmount());
        bloodInformation.setApplyId(applyId);
        returnBloodInformationMapper.insert(bloodInformation);
    }

    @Override
    public void addApplication(ReturnBlood returnBlood) {
        String hospitalName = bloodTransferService.getHospitalName(returnBlood.getRelatedId());
        returnBlood.setApplyHospitalName(hospitalName);
        returnBloodMapper.insert(returnBlood);
        //更新review
        System.out.println("111111111");
        ReturnBloodReview returnBloodReview = new ReturnBloodReview();
        returnBloodReview.setReturnBloodId(returnBlood.getApplyId());
        returnBloodReview.setApplicantId(returnBlood.getApplicantId());
        returnBloodReview.setApplyTime(returnBlood.getApplyTime());
        returnBloodReview.setState(returnBlood.getState());
        reviewMapper.insert(returnBloodReview);
        System.out.println("22222222222");

    }

    @Override
    public Boolean wholeReturn(ReturnBlood returnBlood) {
        return null;
    }

    @Override
    public Boolean partReturn(ReturnBlood returnBlood) {
        return null;
    }

    @Override
    public void deleteOne(String string) {
        LambdaQueryWrapper<ReturnBlood> queryWrapper = new LambdaQueryWrapper<ReturnBlood>();
        queryWrapper.eq(ReturnBlood::getApplyId,string);
        returnBloodMapper.delete(queryWrapper);
        //review
        LambdaQueryWrapper<ReturnBloodReview> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(ReturnBloodReview::getReturnBloodId,string);
        reviewMapper.delete(queryWrapper1);

    }

    @Override
    public List<ReturnBlood> getAll() {

        LambdaQueryWrapper<ReturnBlood> queryWrapper = new LambdaQueryWrapper<ReturnBlood>();
        queryWrapper.eq(ReturnBlood::getState,"待审核");

        return returnBloodMapper.selectList(queryWrapper);
    }
}
